<template>
  <div class="rights">
    <!-- 1、面包屑导航  -->
    <el-breadcrumb :separator-icon="ArrowRight">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 2.卡片 -->
    <el-card>
      <!-- 2-1 权限列表 -->
      <el-table
        :data="rights"
        border
        stripe
        fit
        :header-cell-style="header_cell_style"
        style="width: 100%"
      >
        <el-table-column type="index" label="序号" width="80" align="center" />
        <el-table-column prop="authName" label="权限名称" align="center" />
        <el-table-column prop="path" label="权限路径" align="center" />
        <el-table-column prop="path" label="权限路径" align="center">
          <template #default="props">
            <el-tag v-if="props.row.level == '0'">一级权限</el-tag>
            <el-tag v-if="props.row.level == '1'" type="success"
              >二级权限</el-tag
            >
            <el-tag v-if="props.row.level == '2'" type="warning"
              >三级权限</el-tag
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 2-2 分页器 -->
      <el-pagination
        :currentPage="pagenum"
        :page-size="pagesize"
        :page-sizes="[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup>
/*---------------------- 引入资源 ---------------------- */
import { ref, onMounted, computed } from "vue";
import { ArrowRight } from "@element-plus/icons-vue";
import { error } from "@/utils/message";
import request from "@/utils/request";
/*---------------------- 定义变量 ---------------------- */
let header_cell_style = {
    backgroundColor: "#00A0ff",
    color: "#fff",
    textAlign: "center",
  }, // 表格头部的单元格样式
  rightsList = ref([]),
  total = ref(0), // 角色的总数
  pagenum = ref(1), // 当前页码
  pagesize = ref(5), // 一页几条
  rights = computed(() => {
    let s = (pagenum.value - 1) * pagesize.value,
      e = s + pagesize.value;
    if (e > total.value) {
      e = total.value;
    }
    return rightsList.value.slice(s, e);
  });
/*---------------------- 功能函数 ---------------------- */
// 一页几条发生变化执行的操作
function handleSizeChange(size) {
  pagesize.value = size;
  pagenum.value=1
  getRightList();
}
// 页码发生变化时执行的操作
function handleCurrentChange(num) {
  pagenum.value = num;
  getRightList();
}
async function getRightList() {
  let option = {
    url: "rights/list",
    type: "get",
  };
  try {
    let { data: res } = await request(option);
    if (res.meta.status !== 200) {
      return error(res.meta.msg);
    }
    rightsList.value = res.data;
    total.value = res.data.length;
  } catch (error) {
    console.log(error);
  }
}
/*---------------------- 生命周期函数 ---------------------- */
onMounted(() => {
  getRightList();
});
</script>

<style lang="scss" scoped>
.el-card {
  margin: 10px 0;
}
.el-table {
  margin: 20px 0;
}
.el-pagination {
  float: right;
  margin-bottom: 15px;
}
</style>